package net.NingXiaRuiTai.ShortChain.listener;

import lombok.extern.slf4j.Slf4j;
import com.rabbitmq.client.Channel;
import net.NingXiaRuiTai.ShortChain.enums.BizCodeEnum;
import net.NingXiaRuiTai.ShortChain.enums.EventMessageType;
import net.NingXiaRuiTai.ShortChain.exception.BizeException;
import net.NingXiaRuiTai.ShortChain.model.EventMessage;
import net.NingXiaRuiTai.ShortChain.service.ShortLinkService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

@Component
@Slf4j
@RabbitListener(queues = "short_link.add.mapping.queue")
public class ShortLinkAddMappingMQListener {
    @Resource
    private ShortLinkService shortLinkService;
    @RabbitHandler
    public void shortLinkHandler(EventMessage eventMessage, Message message, Channel channel) throws IOException {
        log.info("ShortLinkAddMappingMQListener监听到消息 message消息内容:{}",message);
        long tag = message.getMessageProperties().getDeliveryTag();
        try{

            //TODO 处理业务逻辑
            eventMessage.setEventMessageType(EventMessageType.SHORT_LINK_ADD_MAPPING.name());

            shortLinkService.handAddShortLink(eventMessage);
        }catch (Exception e){

            //处理业务异常，还有进行其他操作，比如记录失败原因
            log.error("ShortLinkAddMappingMQListener消费失败:{}",eventMessage);
            throw new BizeException(BizCodeEnum.MQ_CONSUME_EXCEPTION);
        }
        log.info("ShortLinkAddMappingMQListener消费成功:{}",eventMessage);
        //确认消息消费成功
//        channel.basicAck(tag,false);

    }
}
